<?php
/**
 *
 * @author fanzhanao
 * @version 
 */


class cartoon_model_photo extends cartoon_model_bmodel
{
   /**
     * Constructor: initialize plugin loader
     * 
     * @return void
     */
    public function __construct(){
    	parent::__construct();
        $this->table = 'cartoon_images';
    }
    
    /**
	 * 添加图片信息
	 * @param array $para = array(
	 * 		'img_title'=>'图片标题',
	 * 		'img_cartoon_id'=>'漫画id',
	 * 		'img_chapter_id'=>'漫画章节id'，
	 * 		'img_url'	=>	'图片地址',
	 * 		'img_filesize'	=>	'图片大小',
	 * 		'img_order'		=>	'图片排序'
	 * )
	 */
	public function add_photo($para){
		if($para['img_cartoon_id'] && $para['img_chapter_id'] && $para['img_url']){
			if($max_id = $this->getMaxPid($para['img_chapter_id'])){
				$para['img_order'] = intval($max_id)+1;
			}else $para['img_order'] = 1;
			if($rs = $this->db->insert('cartoon_images',$para)){
				Model::factory('cartoon_model_chapter')->upChapterPhotoNum($para['img_chapter_id'],$para['img_cartoon_id']);
				// 增加缓存
//				MemRow::incrList($rs,array($this,'getPhotoListIds',array($rs)));
				return $rs;
			}
		}
		return false;
	}
	
	public function getMaxPid($chapter_id){
		
		if(!is_numeric($chapter_id)) return false;
		
		return $this->db->fetchField('SELECT MAX(img_order) AS max_id FROM '.$this->table.' WHERE img_chapter_id='.$chapter_id,'max_id');
	}
	
	/**
	 * 获取图片的信息
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	public function getPhotoRow($id){
		if(is_numeric($id)){
			return $this->db->fetchRow("SELECT * FROM cartoon_images WHERE id=$id");
		}
		return false;
	}
	
	public function getPhotoInfo($id){
		return MemRow::mget(PHOTO_INFO,$id,array($this,'getRows'));
	}
	/**
	 * 根据id获取图片信息
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	public function getRows($id){
		$id = is_array($id)?implode(',',$id):$id;
		return $this->db->fetchAll("SELECT * FROM cartoon_images WHERE id IN ($id) ");
	}
	
	/**
	 * 更新图片的访问量
	 *
	 * @param unknown_type $id
	 * @param unknown_type $num
	 */
	public function upPhotoPv($id,$num=1){
		if(!is_numeric($id)) return false;
		return $this->db->query("UPDATE cartoon_images SET img_pv = img_pv+$num WHERE id=$id");
	}
	
/**
	 * 更新漫画章节的漫画数量
	 * @param uint $cartoon_id  漫画id
	 * @param uint $chapter_id  章节id
	 * @param uint $num		更新数量
	 */
	public function upPhotoNum($cartoon_id,$chapter_id,$num=1){
		if(!(is_numeric($cartoon_id) && is_numeric($chapter_id))) return false;
		$sql = "UPDATE $this->chapter SET pic_num = pic_num+$num,up_time=".CURRENT_TIME.' WHERE id='.$chapter_id.' AND cartoon_id='.$cartoon_id;
		return $this->db->query($sql);
	}
	
/**
	 * 删除图片
	 *
	 * @param unknown_type $id
	 * @param unknown_type $chapter_id
	 * @return unknown
	 */
	public function del_photo($id){
		if(!is_numeric($id)) return false;
		/**
		 * 获取图片信息
		 */
		if($photoInfo = $this->getPhotoRow($id)){
			$file = CT_UPLOAD_PATH.$photoInfo['img_url'];
			$_file_info = pathinfo($file);
			$_mid_file = str_replace('.'.$_file_info['extension'],'_mid'.'.'.$_file_info['extension'],$file);
			$_small_file = str_replace('.'.$_file_info['extension'],'_small'.'.'.$_file_info['extension'],$file);
			$_big_file = str_replace('.'.$_file_info['extension'],'_big'.'.'.$_file_info['extension'],$file);
			if(file_exists($file)){
				@unlink($file);//删除文件
			}			
			if(file_exists($_mid_file)){
				@unlink($_mid_file);//删除文件
			}
			if(file_exists($_small_file)){
				@unlink($_small_file);//删除文件
			}
			if(file_exists($_big_file)){
				@unlink($_big_file);//删除文件
			}
			
			$chapter_id = $photoInfo['img_chapter_id'];
			
			if($this->db->query('DELETE FROM cartoon_images WHERE id='.$id)){
				Model::factory('cartoon_model_chapter')->upChapterPhotoNum($chapter_id);
				MemRow::decrList($rs,array($this,'getPhotoListIds',array($rs)));
				return true;
			}
			
		}
	}
	
	/**
	 * 获取图片列表
	 * @param unknown_type $chapter_id
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getPhotoList($chapter_id){
		if(!is_numeric($chapter_id)) return false;
		$sql = "SELECT * FROM $this->table WHERE img_chapter_id=$chapter_id ORDER BY img_title + 0";
		return $this->db->fetchAll($sql);
	}
	
	/**
	 * 获取图片列表
	 * @param unknown_type $chapter_id
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getPhotoListPage($chapter_id,$page=1,$pageSize=16){
		if(!is_numeric($chapter_id)) return false;
		$sql = "SELECT * FROM $this->table WHERE img_chapter_id=$chapter_id ORDER BY img_title + 0";
		return $this->db->selectPage($sql,array(),$page,$pageSize);
	}
	
	/**
	 * 获取图片的id列表
	 *
	 * @param  int $chapter_id
	 * @return  int
	 */
	public function getPhotoListIds($chapter_id){	
		$return = array();
		$sql = "select id from ".$this->table." where img_chapter_id= $chapter_id img_title + 0";
		if($ids = $this->db->fetchAll($sql)){
			foreach ($ids as $id){
				array_push($return, $id['id']);
			}
		}
		return $return;
	}
	

	
	
}

